package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.app.Card;
import com.ruoyi.system.service.CardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Api(tags = "电卡相关")
@RestController
@RequestMapping
public class CardController extends BaseController {

    @Autowired
    CardService cardService;

    @ApiOperation(value = "电卡导入",notes = "system:card:import")
    @PreAuthorize("@ss.hasPermi('system:card:import')")
    @PostMapping("/card/import")
    public AjaxResult cardImport(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Card> util = new ExcelUtil<Card>(Card.class);
        List<Card> cardList = util.importExcel(file.getInputStream());
        String message = cardService.importUser(cardList, updateSupport);
        return AjaxResult.success(message);
    }

    @ApiOperation(value = "电卡入库模板",notes = "/card/template")
    @GetMapping("/card/template")
    public AjaxResult batchCellTemplate(){
        ExcelUtil<Card> util = new ExcelUtil<Card>(Card.class);
        return util.importTemplateExcel("电卡数据");
    }

    @ApiOperation(value = "创建电卡",notes = "system:card:add")
    @PreAuthorize("@ss.hasPermi('system:card:add')")
    @PostMapping("/card/add")
    public AjaxResult addCard(@RequestParam String cardNum,@RequestParam Integer cardType,
                              Long operatorId,String operatorName,Long communityId,String communityName,
                              String remark){
        Card card = new Card();
        card.setCardNum(cardNum);
        card.setCardType(cardType);
        card.setOperatorId(operatorId);
        card.setOperatorName(operatorName);
        card.setCommunityId(communityId);
        card.setCommunityName(communityName);
        card.setRemark(remark);
        cardService.addCard(card);
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改电卡",notes = "system:card:set")
    @PreAuthorize("@ss.hasPermi('system:card:set')")
    @PutMapping("/card/set")
    public AjaxResult setCard(@RequestBody  Card card){
        cardService.setCard(card);
        return AjaxResult.success();
    }

    @ApiOperation(value = "删除电卡",notes = "system:card:del")
    @PreAuthorize("@ss.hasPermi('system:card:del')")
    @DeleteMapping("/card/del")
    public AjaxResult delCard(@RequestParam Long cardId){
        cardService.delCard(cardId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "电卡列表",notes = "system:card:list")
    @PreAuthorize("@ss.hasPermi('system:card:list')")
    @GetMapping("/card/list")
    public AjaxResult cardList(Long communityId,String cardNum,String operatorName,String communityName,String userName){
        Card card = new Card();
        card.setCardNum(cardNum);
        card.setUserName(userName);
        card.setOperatorName(operatorName);
        card.setCommunityId(communityId);
        card.setCommunityName(communityName);
        startPage();
        return AjaxResult.success(getDataTable(cardService.cardList(card)));
    }

    @ApiOperation(value = "根据运营商名称获取小区列表",notes = "system:operator:community:list")
    @PreAuthorize("@ss.hasPermi('system:operator:community:list')")
    @GetMapping("/operator/community/list")
    public AjaxResult operatorCommunityList(String operatorName){
        return AjaxResult.success(cardService.operatorCommunityList(operatorName));
    }

    @ApiOperation("电卡手动充值")
    @PreAuthorize("@ss.hasPermi('system:card:charge')")
    @GetMapping("/card/charge")
    public AjaxResult cardCharge(@RequestParam Long cardId, @RequestParam BigDecimal money){
        cardService.cardCharge(cardId,money);
        return AjaxResult.success();
    }
}
